<template>
	<view class="public-follow-groups">
		<template v-if="$_loginOrNot()">
			<template v-if="listGroupState == 'success'">
				<template v-if="myGroup.length">
					<view class="my-group">
						<view
							@click="$_To('group/group?gid=' + item.gid)"
							v-for="item in myGroup"
							class="_for">
							<view class="_image">
								<image :src="item.cover" mode="widthFix"></image>
							</view>
							<text class="text">{{item.gname}}</text>
							<text class="iconfont icon-arrow-right"></text>
						</view>
					</view>
				</template>
				<template v-else>
					<public-empty></public-empty>
				</template>
				<view class="show-hidden" @click="clickShowHidden">显示 & 隐藏</view>
				<template v-if="showHidden">
					<template v-for="item in listGroup">
						<view class="title">{{item.gname}}</view>
						<view
							@click="clickGroups(group.gid)"
							v-for="group in item.groups"
							class="for">
							<view class="left" v-if="group.cover">
								<image :src="group.cover" mode="widthFix"></image>
							</view>
							<view class="center">
								<view class="center-top">{{group.gname}}</view>
								<view class="center-bottom">
									<text>{{group.postCount}}帖子</text>
									<text>{{group.commentCount}}回复</text>
									<text>{{group.canViewContent?'':'无权限'}}</text>
								</view>
							</view>
							<view class="right">
								<text
									v-if="group.interaction.followStatus"
									style="color: #409EFF;">已关注</text>
								<text
									v-else
									style="color: #C0C4CC;">未关注</text>
							</view>
						</view>
					</template>
				</template>
			</template>
			<template v-if="listGroupState == 'loading'">
				<view class="my-group">
					<view class="_for">
						<view class="_image" style="width: 0;"></view>
						<text class="text">Loading...</text>
						<text class="iconfont icon-arrow-right"></text>
					</view>
				</view>
			</template>
			<template v-if="listGroupState == 'empty'">
				<public-empty></public-empty>
			</template>
		</template>
		<template v-else>
			<public-not-logged-in></public-not-logged-in>
		</template>
	</view>
</template>

<script>
	export default {
		name:"public-follow-groups",
		data() {
			return {
				listGroupState:"loading",
				listGroup:[],
				myGroup:[],
				showHidden:false
			};
		},
		mounted(){
			this.getMyGroup();
		},
		methods:{
			getMyGroup(){
				this.$_Request(
					"GET","/api/v2/user/"+uni.getStorageSync('user').data.detail.uid+"/mark/follow/groups",
					{
						"whitelistKeys":"cover,gid,gname"
					},
					(res) => {
						this.myGroup = res.data.data.list;
						this.listGroupState = "success";
						this.getListGroup()
					}
				)
			},
			getListGroup(){
				this.$_Request(
					"GET","/api/v2/group/tree",{},
					(res) => {
						this.listGroup = res.data.data;
					}
				)
			},
			clickGroups(gid){
				uni.showLoading();
				this.$_Request(
					"POST","/api/v2/user/mark",
					{
						"interactionType":"follow",
						"markType":"group",
						"fsid":gid
					},
					(res) => {
						uni.hideLoading();
						if(res.data.code == 0){
							this.getMyGroup();
						}else{
							uni.showToast({
								title: res.data.message,
								icon: "none",
								duration: 1000
							});
						}
					}
				)
			},
			clickShowHidden(){
				this.showHidden = !this.showHidden
			},
		},
	}
</script>

<style lang="scss" scoped>
.public-follow-groups{
	padding-bottom: 15rpx;
	.my-group{
		margin: 25rpx;
		background: $BeiJingSe;
		._for{
			display: flex;
			align-items: center;
			padding: 25rpx;
			border-bottom: 1rpx solid #FFF;
			._image{
				width: 80rpx;
				height: 80rpx;
				overflow: hidden;
				border-radius: 100%;
				margin-right: 15rpx;
				flex-shrink: 0;
			}
			.text{
				flex: 1;
				line-height: 32rpx;
			}
			.icon-arrow-right{
				color: $ZhanWeiSe;
				line-height: 32rpx;
			}
		}
	}
	.show-hidden{
		margin: 25rpx 25rpx 15rpx 25rpx;
		background: $BeiJingSe;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		line-height: 28rpx;
		padding: 30rpx 0;
		border-radius: 100rpx;
	}
	.title{
		text-align: center;
		font-weight: bold;
		margin-top: 25rpx;
	}
	.for{
		display: flex;
		align-items: center;
		margin: 10rpx 25rpx 0 25rpx;
		padding-bottom: 10rpx;
		border-bottom: 1rpx dashed $BianKuangSe;
		.left{
			width: 80rpx;
			height: 80rpx;
			overflow: hidden;
			border-radius: 15rpx;
			margin-right: 15rpx;
			flex-shrink: 0;
		}
		.center{
			flex: 1;
			.center-top{
				font-size: 28rpx;
				line-height: 28rpx;
				padding-top: 15rpx;
				display: -webkit-box;
				/*!autoprefixer:off*/
				-webkit-box-orient: vertical;
				/*autoprefixer:on*/
				-webkit-line-clamp: 1;
				overflow: hidden;
			}
			.center-bottom{
				display: flex;
				align-items: center;
				text{
					color: $ZhanWeiSe;
					font-size: 22rpx;
					padding-right: 10rpx;
				}
			}
		}
		.right{
			display: flex;
			align-items: center;
			font-size: 28rpx;
		}
	}
}
</style>